package com.wc._16届国特冲刺营._05动态规划_状态压缩黑科技.小明的宠物袋;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/5/9 13:53
 * @description
 * https://www.lanqiao.cn/courses/51805/learning/?id=4072905&compatibility=false
 */
public class Main {
    /**
     * 思路:
     * 状压DP
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 35, M = 1010;
    // f[i][j] 表示第 i 行 宠物状态为 j 的最大个数
    static int[][] f = new int[N][M];
    // w[i] 表示 第 i 行的零食状态
    static int[] w = new int[N];
    // state 表示所有行合法的状态
    static int[] state = new int[M * M];
    static int n, m, idx = 0;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();

        for (int i = 0; i < 1 << m; i++) {
            if (check(i)) state[idx++] = i;
        }

        for (int i = 1; i <= n; i++) {
            for (int j = m - 1; j >= 0; j--) {
                int x = sc.nextInt();
                w[i] += x << j;
            }
        }

        for (int i = 1; i <= n + 1; i++) {
            for (int j = 0; j < idx; j++) {
                for (int k = 0; k < idx; k++) {
                    int a = state[j], b = state[k];
                    // 满足上下不冲突, 当前行和零食不冲突
                    if ((a & b) == 0 && (b & w[i]) == 0) {
                        f[i][b] = Math.max(f[i][b], f[i - 1][a] + cnt(b));
                    }
                }
            }
        }
        out.println(f[n + 1][0]);
        out.flush();
    }

    static int cnt(int x) {
        int res = 0;
        for (int i = 0; i < m; i++) {
            res += x >> i & 1;
        }
        return res;
    }


    static boolean check(int x) {
        for (int i = 0; i < m - 1; i++) {
            int a = x >> i & 1, b = x >> (i + 1) & 1;
            if (a == 1 && b == 1) return false;
        }
        return true;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

